import json


class EnvironmentFile:
    def __init__(self, file_path: str = r"./environments.json"):
        self.file_path: str = file_path
        self.env_file_dict: dict = json.load(open(self.file_path, "r"))

    #############################################
    def get_environment_names(self):
        return self.env_file_dict["environments"].keys()

    def get_environment(self, environment_name: str):
        if not self.environment_exist(environment_name):
            raise ValueError(f"Environment '{environment_name}' does not exist")
        return self.env_file_dict["environments"][environment_name]

    def environment_exist(self, environment_name: str):
        return environment_name in self.get_environment_names()

    def save(self):
        json.dump(self.env_file_dict, open(self.file_path, "w"), indent=4)
        return self

    @staticmethod
    def environment_format(env: dict) -> str:
        var = ""
        for key, value in env["variables"].items():
            var += f"\t{key}:\n"
            for v in value:
                var += f"\t\t{v}\n"
        return (
                "Environment Description:\n"
                f"\t{env["description"]}\n"
                "Environment Variables:\n"
                + var
        )

    #############################################
    def create_environment(self, environment_name: str):
        if self.environment_exist(environment_name):
            raise ValueError(f"Environment '{environment_name}' already exists")
        self.env_file_dict["environments"][environment_name] = {
            "description": "",
            "variables": {}
        }
        self.save()
        return self

    def delete_environment(self, environment_name: str):
        if not self.environment_exist(environment_name):
            raise ValueError(f"Environment '{environment_name}' does not exist")
        del self.env_file_dict["environments"][environment_name]
        self.save()
        return self

    def append_variable(self, environment_name: str, variable_name: str, variable_value: str):
        if not self.environment_exist(environment_name):
            raise ValueError(f"Environment '{environment_name}' does not exist")
        if variable_name not in self.env_file_dict["environments"][environment_name]["variables"].keys():
            self.env_file_dict["environments"][environment_name]["variables"][variable_name] = []
        self.env_file_dict["environments"][environment_name]["variables"][variable_name].append(variable_value)
        self.save()
        return self

    def remove_variable(self, environment_name: str, variable_name: str, variable_value: str):
        if not self.environment_exist(environment_name):
            raise ValueError(f"Environment '{environment_name}' does not exist")
        if variable_name not in self.env_file_dict["environments"][environment_name]["variables"].keys():
            raise ValueError(f"Variable '{variable_name}' does not exist")
        self.env_file_dict["environments"][environment_name]["variables"][variable_name].remove(variable_value)
        self.save()
        return self

    def set_description(self, environment_name: str, description: str):
        if not self.environment_exist(environment_name):
            raise ValueError(f"Environment '{environment_name}' does not exist")
        self.env_file_dict["environments"][environment_name]["description"] = description
        self.save()
        return self
